package com.tianqu.tms.www.finance.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.finance.controller.FinanceDailyReportController;
import com.tianqu.tms.www.finance.viewhelper.FinanceDailyReportViewHelper;

public class FinanceDailyExportExcelAction extends TmsAction {
	private Logger logger = Logger.getLogger(FinanceDailyExportExcelAction.class);
	private static String EL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		String date = uc.getParameter("date");
		//check
		if (StringUtil.isNullOrBlank(date)) {
			setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		Pattern p = Pattern.compile(EL);
		Matcher m = p.matcher(date);
		boolean dateFlag = m.matches();
		if (!dateFlag) {
			String[] param = {"对帐日期"};
			setErrorsToRequest(request, TmsConstants.U1014, param); // yyyy-MM-dd
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		FinanceDailyReportViewHelper viewhelper = new FinanceDailyReportViewHelper();
		FinanceDailyReportController controller = new FinanceDailyReportController();
		controller.execute(uc);
		//
		viewhelper = (FinanceDailyReportViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		 
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("车辆查询结果");
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		style.setBorderBottom((short) 1);
		style.setBorderTop((short) 1);
		style.setBorderLeft((short) 1);
		style.setBorderRight((short) 1);

		sheet.setDefaultRowHeightInPoints(10);
		sheet.setDefaultColumnWidth(17);

		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		font.setFontHeightInPoints((short) 8);
		style.setFont(font);

		HSSFCellStyle cellStyle = wb.createCellStyle();
		HSSFFont titleFont = wb.createFont();
		titleFont.setFontName("宋体");
		titleFont.setFontHeight((short) 200);
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

		cellStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		cellStyle.setBorderBottom((short) 1);
		cellStyle.setBorderTop((short) 1);
		cellStyle.setBorderLeft((short) 1);
		cellStyle.setBorderRight((short) 1);
		cellStyle.setFont(titleFont);
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell.setCellStyle(cellStyle);

		HSSFCell cell1 = row.createCell((short) 1);
		cell1.setCellValue("会员编号");
		cell1.setCellStyle(cellStyle);

		HSSFCell cell2 = row.createCell((short) 2);
		cell2.setCellValue("姓名");
		cell2.setCellStyle(cellStyle);

		HSSFCell cell3 = row.createCell((short) 3);
		cell3.setCellValue("所属商户");
		cell3.setCellStyle(cellStyle);

		HSSFCell cell4 = row.createCell((short) 4);
		cell4.setCellValue("今日业务量");
		cell4.setCellStyle(cellStyle);

		HSSFCell cell5 = row.createCell((short) 5);
		cell5.setCellValue("应支付");
		cell5.setCellStyle(cellStyle);

		HSSFCell cell6 = row.createCell((short) 6);
		cell6.setCellValue("实际支付");
		cell6.setCellStyle(cellStyle);

		HSSFCell cell7 = row.createCell((short) 7);
		cell7.setCellValue("今日充值");
		cell7.setCellStyle(cellStyle);

		HSSFCell cell8 = row.createCell((short) 8);
		cell8.setCellValue("今日余额");
		cell8.setCellStyle(cellStyle);

		HSSFCell cell9 = row.createCell((short) 9);
		cell9.setCellValue("差异预警");
		cell9.setCellStyle(cellStyle);

		List<TransferAcceptedInfo> list = viewhelper.getDailyInfo();
		for (int i=1;i<list.size()+1;i++) {
			TransferAcceptedInfo dto = (TransferAcceptedInfo) list.get(i-1);
			row = sheet.createRow(i);
			HSSFCell c0 = row.createCell((short) 0);
			c0.setCellStyle(style);
			c0.setCellValue(dto.getNo());
			HSSFCell c1 = row.createCell((short) 1);
			c1.setCellStyle(style);
			c1.setCellValue(dto.getAttnId());
			HSSFCell c2 = row.createCell((short) 2);
			c2.setCellStyle(style);
			c2.setCellValue(dto.getAttnName());
			HSSFCell c3 = row.createCell((short) 3);
			c3.setCellStyle(style);
			c3.setCellValue(dto.getShortName());
			
			HSSFCell c4 = row.createCell((short) 4);
			c4.setCellStyle(style);
			c4.setCellValue(StringUtil.isNotBlank(dto.getDailyCount())?Integer.parseInt(dto.getDailyCount()):0);
			
			HSSFCell c5 = row.createCell((short) 5);
			c5.setCellStyle(style);
			c5.setCellValue(StringUtil.isNotBlank(dto.getDailyPayment())?Double.parseDouble(dto.getDailyPayment()):0);
			
			HSSFCell c6 = row.createCell((short) 6);
			c6.setCellStyle(style);
			c6.setCellValue(StringUtil.isNotBlank(dto.getDailyPaymented())?Double.parseDouble(dto.getDailyPaymented()):0);
			
			HSSFCell c7 = row.createCell((short) 7);
			c7.setCellStyle(style);
			c7.setCellValue(StringUtil.isNotBlank(dto.getDailyTransfer())?Double.parseDouble(dto.getDailyTransfer()):0);
			
			HSSFCell c8 = row.createCell((short) 8);
			c8.setCellStyle(style);
			c8.setCellValue(StringUtil.isNotBlank(dto.getDailyRemain())?Double.parseDouble(dto.getDailyRemain()):0);
			
			HSSFCell c9 = row.createCell((short) 9);
			c9.setCellStyle(style);
			c9.setCellValue(dto.getDailyWarning());
		}
		try {
			File file = new File(toUtf8String("每日对帐查询结果" + date + ".xls"));
			OutputStream fout = new FileOutputStream(file);
			wb.write(fout);
			getResponse(file, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		return null;
	}

}
